/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.whatsnew.internal.xwikiblog;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.xwiki.whatsnew.NewsCategory;

/**
 * Converts News Categories from their definition in the RSS content generated by the XWiki Blog Application and the
 * {@link NewsCategory} format, and from the later to the format used in the query string to the Blog Application's
 * RSS URL.
 *
 * @version $Id$
 * @since 15.2RC1
 */
public class XWikiBlogNewsCategoryConverter
{
    private static final String CATEGORY_SIMPLE_USER = "Blog.What's New for XWiki: Simple User";

    private static final String CATEGORY_ADMIN_USER = "Blog.What's New for XWiki: Admin User";

    private static final String CATEGORY_ADVANCED_USER = "Blog.What's New for XWiki: Advanced User";

    private static final String CATEGORY_EXTENSION = "Blog.What's New for XWiki: Extension";

    /**
     * Converts News Categories from their definition in the RSS content generated by the XWiki Blog Application and the
     * {@link NewsCategory} format.
     *
     * @param rssCategories the list of RSS categories
     * @return the collection of News Categories as {@link NewsCategory} objects
     */
    public Set<NewsCategory> convertFromRSS(List<String> rssCategories)
    {
        Set<NewsCategory> mappedCategories = new HashSet<>();
        for (String category : rssCategories) {
            // Try to find a matching category
            if (CATEGORY_ADMIN_USER.equals(category)) {
                mappedCategories.add(NewsCategory.ADMIN_USER);
            } else if (CATEGORY_SIMPLE_USER.equals(category)) {
                mappedCategories.add(NewsCategory.SIMPLE_USER);
            } else if (CATEGORY_ADVANCED_USER.equals(category)) {
                mappedCategories.add(NewsCategory.ADVANCED_USER);
            } else if (CATEGORY_EXTENSION.equals(category)) {
                mappedCategories.add(NewsCategory.EXTENSION);
            } else {
                mappedCategories.add(NewsCategory.UNKNOWN);
            }
        }
        return mappedCategories;
    }

    /**
     * Convert News Categories, from the {@link NewsCategory} format to the format used in the query string to the
     * Blog Application's RSS URL.
     *
     * @param categories the news categories to convert
     * @return the query string portion representing the categories
     */
    public String convertToQueryString(Set<NewsCategory> categories)
    {
        StringBuilder builder = new StringBuilder();
        Iterator<String> iterator = getMappedCategoriesForURL(categories).iterator();
        while (iterator.hasNext()) {
            builder.append("category").append('=').append(encodeString(iterator.next()));
            if (iterator.hasNext()) {
                builder.append('&');
            }
        }
        return builder.toString();
    }

    private String encodeString(String text)
    {
        return URLEncoder.encode(text, StandardCharsets.UTF_8);
    }

    private List<String> getMappedCategoriesForURL(Set<NewsCategory> categories)
    {
        // TODO: Until https://jira.xwiki.org/browse/BLOG-198 is implemented, the XWiki Blog app only supports
        // a single category. Thus, FTM, we use the "Blog.What's New for XWiki" one to get all news under that
        // category.
        // Once it's fixed, uncomment the following line:
        /*
        List<String> results = new ArrayList<>();
        for (NewsCategory category : categories) {
            String categoryString;
            if (NewsCategory.SIMPLE_USER.equals(category)) {
                categoryString = CATEGORY_SIMPLE_USER;
            } else if (NewsCategory.ADVANCED_USER.equals(category)) {
                categoryString = CATEGORY_ADVANCED_USER;
            } else if (NewsCategory.ADMIN_USER.equals(category)) {
                categoryString = CATEGORY_ADMIN_USER;
            } else if (NewsCategory.EXTENSION.equals(category)) {
                categoryString = CATEGORY_EXTENSION;
            } else {
                categoryString = null;
            }
            if (categoryString != null) {
                results.add(categoryString);
            }
        }
         */
        return List.of("Blog.What's New for XWiki");
    }
}
